﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.BaseControl = function(element) {
    YUIAspNet.BaseControl.initializeBase(this, [element]);
}

YUIAspNet.BaseControl.prototype = {
    initialize: function() {
        YUIAspNet.BaseControl.callBaseMethod(this, 'initialize');

        this._parent = null;

        var beforeInit = null;
        var afterInit = null;

        if (this._events) {
            beforeInit = this._events.getHandler("onBeforeInitialization");
            afterInit = this._events.getHandler("onAfterInitialization");
        }

        var currentControl = this;

        if (beforeInit)
            Sys.Application.add_load(function() { beforeInit(currentControl, null); Sys.Application.remove_load(arguments.callee); });

        if (this.get_AutoInitialize()) {
            //Function.createDelegate(this, this.initializeYUIControl)
            Sys.Application.add_load(function() { currentControl.initializeYUIControl(); Sys.Application.remove_load(arguments.callee); });
        }

        if (afterInit)
            Sys.Application.add_load(function() { afterInit(currentControl, null); Sys.Application.remove_load(arguments.callee); });
    },

    //will be overriden by super class
    initializeYUIControl: function() { },

    get_YUIControl: function() {
        return this._yuiControl;
    },
    
    get_StateFieldID: function() {
        return this.get_element().id + "_state";
    },

    get_AutoInitialize: function() {
        return this._autoInitialize;
    },
    set_AutoInitialize: function(autoInitialize) {
        this._autoInitialize = autoInitialize;
    },
    
    get_Effect: function() {
        return this._effect;
    },
    set_Effect: function(effect) {
        this._effect = effect;
    },

    get_Parent: function() {
        return this._parent;
    },
    set_Parent: function(parent) {
        this._parent = parent;
    },

    get_DataSource: function() {
        this._yuiDataSource = null;

        if (!this.get_Source().URL && !this.get_Source().ElementID) { // local
            this._yuiDataSource = new YAHOO.util.DataSource(this.get_Source().Data);

            this._yuiDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
        }
        else if(this.get_Source().URL) { // remote
            var url = this.get_Source().URL;
            //put ? or & in the end
            if (url.indexOf("?") > -1) {
                if (url[url.length - 1] != "&")
                    url += "&";
            }
            else
                url += "?";

            this._yuiDataSource = new YAHOO.util.XHRDataSource(url);

            this._yuiDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        }
        else if(this.get_Source().ElementID) {
            $get(this.get_Source().ElementID).style.display='none'; // js is enabled, we make the source table invisible.
            this._yuiDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get(this.get_Source().ElementID));

            this._yuiDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
            
            // remove first line from results. asp.net grid keeps columns in tr.
            this._yuiDataSource._oldparseHTMLTableData = this._yuiDataSource.parseHTMLTableData;
            var src = this._yuiDataSource;
            this._yuiDataSource.parseHTMLTableData = function(oRequest , oFullResponse )
            {
                var respRaw = src._oldparseHTMLTableData(oRequest, oFullResponse);
                
                respRaw.results = respRaw.results.splice(1, respRaw.results.length-1);
                
                return respRaw;                
            }
        }

        //meta fields?


        this._yuiDataSource.responseSchema =
	        {
	            fields: this.get_Source().Columns,
	            resultsList: "Data", // this property will be handled only in the YUIAsp.Net, never outside it.
	            metaFields: this.get_Source().Meta
	        };
        return this._yuiDataSource;
    },

    get_Source: function() {
        return this._source;
    },
    set_Source: function(source) {
        this._source = source;
    },

    DoPostback: function(arg) {
        __doPostBack(this.get_element().id, arg);
    },
    
    _getEffect: function() {
        var effect = {};
        
        if(this.get_Effect())
        {
            if(this.get_Effect().Effect == 0)
                effect.effect = YAHOO.widget.ContainerEffect.FADE;
            else
                effect.effect = YAHOO.widget.ContainerEffect.SLIDE;
                    
            effect.duration = this.get_Effect().Duration;
            return effect;
        }
        
        return null;
    },

    add_onBeforeInitialization: function(handler) {
        this.get_events().addHandler("onBeforeInitialization", handler);
    },

    remove_onBeforeInitialization: function(handler) {
        this.get_events().removeHandler("onBeforeInitialization", handler);
    },

    add_onAfterInitialization: function(handler) {
        this.get_events().addHandler("onAfterInitialization", handler);
    },

    remove_onAfterInitialization: function(handler) {
        this.get_events().removeHandler("onAfterInitialization", handler);
    },

    dispose: function() {
        $clearHandlers(this.get_element());

        //Add custom dispose actions here
        YUIAspNet.BaseControl.callBaseMethod(this, 'dispose');
    }

}
YUIAspNet.BaseControl.registerClass('YUIAspNet.BaseControl', Sys.UI.Control);
